open(BUFF,"cut -f1-4 -d ' ' /data1/bsi/refdata/genetics/1000Genomes/processed_data/release/2010_11/plink_tped/all_chr/all_chr.tped |");
while($line = <BUFF>)
{
	chomp($line);
	@lin = split(" ",$line);
	$hash{"$lin[0]_$lin[3]"} = $lin[1];
}
for($i=0;$i<@ARGV;$i++)
{
	        chomp($ARGV[$i]);
		$buff = "BUFF$i";
		open $buff, "<$ARGV[$i]" or die "can't open the $ARGV[$i] file\n";
		@arrr = split(/\//,$ARGV[$i]);
		@arr = split(/\./,$arrr[@arrr-1]); 
		$array[$i] = $arr[0];
}
#print "naresh @array\n";
open(WRBUFF,"> /data1/bsi/bioinf_int/s106381.borawork/Chien_data/final.tfam");
open(WRBUFF1,"> /data1/bsi/bioinf_int/s106381.borawork/Chien_data/final.tped");
open(HIST,"> /data1/bsi/bioinf_int/s106381.borawork/Chien_data/histogram.txt");
for($i=0;$i<@ARGV;$i++)
{
	print WRBUFF "$array[$i] $array[$i] 0 0 0 0\n";
}
$num_tri =0;
@base = ("A","C","G","T");
system("cat /data1/bsi/bioinf_int/s106381.borawork/Chien_data/final.tfam");
while($line = <BUFF0>)
{
	chomp($line);
	#print WRBUFF "$array[0] $array[0] 0 0 0 0\n";
	@lin = split(" ",$line);
	if(exists($hash{"$lin[0]_$lin[3]"}))
	{
		$lin[1] = $hash{"$lin[0]_$lin[3]"};
	}
	else
	{
		$lin[1] = "tk37_".$lin[1];
	}
	if($lin[4] =~ m/_/ || $lin[5] =~ m/_/)
         {
                        $lin[4] = "0 0";
         }

                     
	$line = join(" ",@lin);
	for($i=1;$i<@ARGV;$i++)
	{
		
		$buff = "BUFF$i";	
		$line1 = <$buff>;
		@arr1 = split(" ",$line1);
		if($arr1[4] =~ m/_/ || $arr1[5] =~ m/_/)
		{
			$line = $line." 0 0";
		}
		else
		{
			$line = $line." $arr1[4] $arr1[5]";	
		}
	}
	$k =0;
	if($line =~ m/A/)
	{
		$k++;
	}
	 if($line =~ m/C/)
        {
		$k++;
        }
	 if($line =~ m/G/)
        {
		$k++;
        }
	 if($line =~ m/T/)
        {
		$k++;
        }
	if($k > 2)
	{
		@basecount = (0,0,0,0);
		$basecount[0] = () = $line =~ /A/g;
                $basecount[1] = () = $line =~ /C/g;
                $basecount[2] = () = $line =~ /G/g;
                $basecount[3] = () = $line =~ /T/g;	
		@index1 = sort{ $basecount[ $b ] <=> $basecount[ $a ] } 0 .. $#basecount;
#		print $line."\n";
#		print "@basecount\n@index1\n";
		@lines = split(" ",$line);
		for($j=4;$j<@lines;$j++)
		{
			$a1 = $lines[$j];
			$j++;
			$a2 = $lines[$j];
			$aa = "$a1 $a2";
			#if($a1 eq $base ||$index1[2]] || $a1 eq $base[$index1[3]] || $a2 eq $base[$index1[2]] || $a2 eq $base[$index1[3]] ) 
			if($aa =~ /$base[$index1[2]]/ || $aa =~ /$base[$index1[3]]/) 
			{
				$lines[$j] = "0";
				 $lines[$j-1] = "0";
			}
		#	$j++;
		}
		$line = join(" ",@lines);
#		print "$line\n";
		$num_tri++;
	}
	$histcount = () = $line =~ / 0 0/g;
	print HIST $histcount."\n";
	print WRBUFF1 $line."\n";
}
print "total number of triallels $num_tri\n";
